<?php
/*
 * @Author: an
 * @Date: 2021/12/8
 * @Description: URL鉴权
 */


namespace App\Services\Aliyun\Live;


use App\Services\Aliyun\BaseService;
use App\Services\Configs\DefaultService;

class AuthKeyService extends BaseService
{
    private $expire_in = 86400;//鉴权过期时间
    private $pushHost;//推流域名，控制台生成的
    private $playHost;//播放域名，控制台生成的
    private $pushKey;//推流地址鉴权key，控制台生成的
    private $playKey;//播放地址鉴权key，控制台生成的

    public function __construct()
    {
        $config = $this->getConfig(DefaultService::ALI_LIVE);
        $this->pushHost = $config['pushHost'];
        $this->playHost = $config['playHost'];
        $this->pushKey = $config['pushKey'];
        $this->playKey = $config['playKey'];
    }

    /**
     * 获取推流地址
     * @param $appName
     * @param $streamName
     * @return string
     * @author an
     * @time 2021-12-09 16:02
     */
    public function pushUrl($appName, $streamName)
    {
        //未开启鉴权Key的情况下
        if ($this->pushKey == '') {
            $push_url = 'rtmp://' . $this->pushHost . '/' . $appName . '/' . $streamName;
            return $push_url;
        }
        //开启鉴权Key的情况下
        $timeStamp = time() + $this->expire_in;
        $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $this->pushKey;
        $md5hash = md5($sstring);
        $push_url = 'rtmp://' . $this->pushHost . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
        return $push_url;
    }

    /**
     * 批量获取播放地址
     * @param $appName
     * @param $streamName
     * @return array
     * @author an
     * @time 2021-12-09 16:01
     */
    public function playUrl($appName, $streamName)
    {
        //未开启鉴权Key的情况下
        if ($this->playKey == '') {
            $rtmp_play_url = 'rtmp://' . $this->playHost . '/' . $appName . '/' . $streamName;
            $flv_play_url = 'http://' . $this->playHost . '/' . $appName . '/' . $streamName . '.flv';
            $hls_play_url = 'http://' . $this->playHost . '/' . $appName . '/' . $streamName . '.m3u8';
        } else {
            $timeStamp = time() + $this->expire_in;

            $rtmp_sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $this->playKey;
            $rtmp_md5hash = md5($rtmp_sstring);
            $rtmp_play_url = 'rtmp://' . $this->playHost . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmp_md5hash;

            $flv_sstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $this->playKey;
            $flv_md5hash = md5($flv_sstring);
            $flv_play_url = 'http://' . $this->playHost . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flv_md5hash;

            $hls_sstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $this->playKey;
            $hls_md5hash = md5($hls_sstring);
            $hls_play_url = 'http://' . $this->playHost . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hls_md5hash;
        }
        return [
            'rtmp' => $rtmp_play_url,
            'flv' => $flv_play_url,
            'hls' => $hls_play_url,
        ];
    }

    /**
     * @param $expire_in
     * @author an
     * @time 2021-12-09 16:41
     */
    public function setExpireTime($expire_in)
    {
        $this->expire_in = $expire_in;
    }
}
